package pl.koziolekweb.szkola.ERLists;

// ER-Lists/AList.java
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;

public class AList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("Sue");
        list.add("Lea");
        list.add("Ann");
        list.add("Kim");
        list.add("Lea");

        // "traditional" looping
        System.out.print("List has " +
                         list.size() + " elements:   ");
        for (int i = 0; i < list.size(); ++i)
            System.out.print(" " + list.get(i));
        System.out.println();

        // "new" looping
        System.out.print("Another way of looping:");
        for (String s : list) System.out.print(" " + s);
        System.out.println();

        // iterator
        System.out.print("And now with iterator: ");
        Iterator<String> iter = list.iterator();
        while (iter.hasNext())
            System.out.print(" " + iter.next());
        System.out.println();

        // sorting
        Collections.sort(list);
        printColl("Sorted collection:     ",list);

        // HashSet
        Set<String> hSet = new HashSet<String>(list);
        printColl("Hash set:              ",hSet);
        // TreeSet
        Set<String> tSet = new TreeSet<String>(list);
        printColl("Tree set:              ",tSet);
    }

    static void printColl(String msg, Collection<?> col) {
        System.out.print(msg);
        for (Object c : col) System.out.print(" " + c);
        System.out.println();
    }
}
